import bar01 from "@/js/element/bar01.js";
import bar02 from "@/js/element/bar02.js";
import bar03 from "@/js/element/bar03.js";
import bar04 from "@/js/element/bar04.js";
import bar05 from "@/js/element/bar05.js";
import bar06 from "@/js/element/bar06.js";
import box from "@/js/element/box.js";
import bubble from "@/js/element/bubble.js";
import button from "@/js/element/button.js";
import calendar from "@/js/element/calendar.js";
import carouselText from "@/js/element/carouselText.js";
import clock from "@/js/element/clock.js";
import cloudMap from "@/js/element/cloudMap.js";
import decoration from "@/js/element/decoration.js";
import funnle from "@/js/element/funnle.js";
import heat from "@/js/element/heat.js";
import histogram from "@/js/element/histogram.js";
import holo3dMap from "@/js/element/holo3dMap.js";
import { setList } from "@/js/element/icon.js";
import iframe from "@/js/element/iframe.js";
import line01 from "@/js/element/line01.js";
import line02 from "@/js/element/line02.js";
import line03 from "@/js/element/line03.js";
import line04 from "@/js/element/line04.js";
import line05 from "@/js/element/line05.js";
import line06 from "@/js/element/line06.js";
// import line07 from '@/js/element/line07.js';
import menu from "@/js/element/menu.js";
import mixed from "@/js/element/mixed.js";
import panel01 from "@/js/element/panel01.js";
import picture from "@/js/element/picture.js";
import pie01 from "@/js/element/pie01.js";
import pie02 from "@/js/element/pie02.js";
import pie03 from "@/js/element/pie03.js";
import pie04 from "@/js/element/pie04.js";
import polarBar from "@/js/element/polarBar.js";
import radar from "@/js/element/radar.js";
import relationship from "@/js/element/relationship.js";
import sankey from "@/js/element/sankey.js";
import scatter3D from "@/js/element/scatter3D.js";
import scatterDiagram from "@/js/element/scatterDiagram.js";
import shape from "@/js/element/shape.js";
import simple from "@/js/element/simple.js";
import statistic from "@/js/element/statistic.js";
import surface from "@/js/element/surface.js";
// import globeLayer from '@/js/element/globeLayer.js';
// import line3d from '@/js/element/line3d.js';
// import graphGL01 from '@/js/element/graphGL01.js';
// import heatmap01 from '@/js/element/heatmap01.js';
// import group01 from '@/js/element/group01.js';
// import parallel01 from '@/js/element/parallel01.js';
// import treemap01 from '@/js/element/treemap01.js';
// import pictorialBar01 from '@/js/element/pictorialBar01.js';
// import themeRiver01 from '@/js/element/themeRiver01.js';
// import bar07 from '@/js/element/bar07.js';
// import bar08 from '@/js/element/bar08.js';
// import bar09 from '@/js/element/bar09.js';
// import candlestick01 from '@/js/element/candlestick01.js';
// import boxplot01 from '@/js/element/boxplot01.js';
import table from "@/js/element/table.js";
import text from "@/js/element/text.js";
import time from "@/js/element/time.js";
import timing from "@/js/element/timing.js";
import tree from "@/js/element/tree.js";
import video from "@/js/element/video.js";
// 新增通用组件：单选框
// import radio from '@/js/element/radio.js';
// import checkbox from '@/js/element/checkbox.js';
// import select from '@/js/element/select.js';
// import cascader from '@/js/element/cascader.js';
// import slider from '@/js/element/slider.js';
// import transfer from '@/js/element/transfer.js';
// import form from '@/js/element/form.js';
// import progress from '@/js/element/progress.js';
// import tabs from '@/js/element/tabs.js';
// import breadcrumb from '@/js/element/breadcrumb.js';
// import dropdown from '@/js/element/dropdown.js';
// import steps from '@/js/element/steps.js';
// import timeline from '@/js/element/timeline.js';
// import divider from '@/js/element/divider.js';
// import collapse from '@/js/element/collapse.js';
// import inputNumber from '@/js/element/inputNumber.js';
// import pagination from '@/js/element/pagination.js';
// import timePicker from '@/js/element/timePicker.js';

import rtspVideo from "@/js/element/rtspVideo.js";
import { setListF } from "@/js/element/function.js";
import html from "@/js/element/html.js";

import url from "@/js/util/url.js";
import user from "@/js/util/user.js";
import config from "@/js/util/config.js";
import util from "@/js/util/util.js";
import elementTree from "@/js/main/tree.js";
import simpleIcon from "../../../src/image/main/toolbar/icon_pop_holo3D.png";
import objectTree from "@/js/element/objectTree.js";
import gantt from "@/js/element/gantt.js";

let template = {
  message: null,
  baseElementList: [],
  baseElementMap: {},
  imageElementList: elementTree
    .filter((item) => item.label === "多媒体")[0]
    .children.filter((item) => item.label === "图片")[0].children,
  videoElementList: elementTree
    .filter((item) => item.label === "多媒体")[0]
    .children.filter((item) => item.label === "视频")[0].children,
  // simpleList: elementTree.filter((item) => item.label === '自定义')[0].children,
  addBaseElement: function (element) {
    this.baseElementList.splice(this.baseElementList.length, 0, element);
    return this;
  },
  removeBaseElementById: function (id) {
    let success = false;
    for (let i = 0; i < this.baseElementList.length; i++) {
      if (this.baseElementList[i].id === id) {
        this.baseElementList.splice(this.baseElementList.length, 1);
        success = true;
        break;
      }
    }
    return success;
  },
  getBaseElementList: function () {
    return this.baseElementList;
  },
  initialize: function () {
    for (let i = this.baseElementList.length - 1; i >= 0; i--) {
      this.baseElementList.splice(i, 1);
    }
    let templateList = [
      ...text,
      ...shape,
      ...setList(),
      ...button,
      ...bar01,
      ...bar02,
      ...bar03,
      ...bar04,
      ...pie01,
      ...line01,
      ...line02,
      ...line03,
      ...radar,
      ...panel01,
      ...polarBar,
      ...mixed,
      ...line04,
      ...sankey,
      ...pie02,
      ...bubble,
      ...pie03,
      ...pie04,
      ...bar05,
      ...bar06,
      ...line05,
      ...line06,
      // ...line07,
      ...funnle,
      ...scatterDiagram,
      ...surface,
      // ...globeLayer,
      // ...line3d,
      // ...graphGL01,
      // ...heatmap01,
      // ...group01,
      // ...parallel01,
      // ...treemap01,
      // ...pictorialBar01,
      // ...themeRiver01,
      // ...bar07,
      // ...bar08,
      // ...bar09,
      // ...candlestick01,
      // ...boxplot01,
      ...histogram,
      ...scatter3D,
      ...heat,
      ...relationship,
      ...time,
      ...clock,
      ...timing,
      ...tree,
      ...calendar,
      ...statistic,
      ...carouselText,
      ...table,
      ...box,
      ...decoration,
      ...cloudMap,
      ...holo3dMap,
      ...menu,
      ...iframe,
      // 新增 通用组件
      // ...radio,
      // ...checkbox,
      // ...select,
      // ...cascader,
      // ...slider,
      // ...transfer,
      // ...form,
      // ...progress,
      // ...tabs,
      // ...breadcrumb,
      // ...dropdown,
      // ...steps,
      // ...timeline,
      // ...divider,
      // ...collapse,
      // ...inputNumber,
      // ...pagination,
      // ...timePicker,
      ...rtspVideo,
      ...setListF(),
      ...html,
      ...objectTree,
      ...gantt,
    ];
    for (let i = 0; i < templateList.length; i++) {
      this.baseElementMap[templateList[i].id] = templateList[i];
      this.baseElementList.splice(i, 0, templateList[i]);
    }
    this.resetResourceList();
    // this.resetSimpleList();
    return this;
  },
  getResourceList: function (type = ["01", "02"]) {
    if (type.length > 0) {
      let str = "";
      for (let i = 0; i < type.length; i++) {
        str += " " + type[i];
      }
      str = str.substring(1);
      let data = url.getResourceList({ type: str, token: user.getToken() });
      if (data.state.code == 10000) {
        return data.body.message.list ? data.body.message.list : [];
      } else {
        return [];
      }
    } else {
      return [];
    }
  },
  getSimpleList: function () {
    let data = url.getSimpleList({ token: user.getToken() });
    if (data.state.code == 10000) {
      return data.body.message.list ? data.body.message.list : [];
    } else {
      return [];
    }
  },
  removeBaseElementByType: function (type) {
    let success = false;
    for (let i = this.baseElementList.length - 1; i >= 0; i--) {
      if (this.baseElementList.component == type) {
        this.baseElementList.splice(i, 1);
        success = true;
      }
    }
    return success;
  },
  resetResourceList: function (type = ["01", "02"]) {
    let resourceList = this.getResourceList(type);
    if (type.indexOf("01") >= 0) {
      for (let i = this.imageElementList.length - 1; i >= 0; i--) {
        this.imageElementList.splice(i, 1);
      }
      this.removeBaseElementByType("Picture");
    }
    if (type.indexOf("02") >= 0) {
      for (let i = this.videoElementList.length - 1; i >= 0; i--) {
        this.videoElementList.splice(i, 1);
      }
      this.removeBaseElementByType("Video");
    }
    if (resourceList && resourceList.length) {
      for (let i = 0; i < resourceList.length; i++) {
        let url = `${config.getServiceURL(
          "scene"
        )}service/scene/file/download?file=${resourceList[i].preview}`;
        if (resourceList[i].type === "01") {
          let template = util.clone(picture);
          template.id = resourceList[i].id;
          template.style.height.value = util.toInteger(
            (resourceList[i].imageheight * template.style.width.value) /
              resourceList[i].imagewidth
          );
          template.style.height.pixel = template.style.height.value;
          template.data.push({
            url: `${resourceList[i].file}`,
            id: resourceList[i].id,
            preview: `${resourceList[i].preview}`,
            width: resourceList[i].imagewidth,
            height: resourceList[i].imageheight,
            filetype: resourceList[i].filetype,
          });
          this.imageElementList.push({
            id: resourceList[i].id,
            component: "Picture",
            icon: url,
            label: "",
          });
          this.baseElementList.splice(i, 0, template);
          this.baseElementMap[template.id] = template;
        } else if (resourceList[i].type === "02") {
          let template = util.clone(video);
          template.id = resourceList[i].id;
          template.data.push({
            url: `${resourceList[i].file}`,
            id: resourceList[i].id,
            preview: `${resourceList[i].preview}`,
            width: resourceList[i].imagewidth,
            height: resourceList[i].imageheight,
            filetype: resourceList[i].filetype,
          });
          this.videoElementList.push({
            id: resourceList[i].id,
            component: "Video",
            icon: url,
            label: "",
          });
          this.baseElementList.splice(i, 0, template);
          this.baseElementMap[template.id] = template;
        }
      }
    }
  },
  resetSimpleList: function () {
    let simpleList = this.getSimpleList();
    let simpleMap = {};
    for (let i = this.simpleList.length - 1; i >= 0; i--) {
      simpleMap[this.simpleList[i].id] =
        this.baseElementMap[this.simpleList[i].id];
      this.simpleList.splice(i, 1);
    }
    this.removeBaseElementByType("Simple");
    if (simpleList && simpleList.length) {
      for (let i = 0; i < simpleList.length; i++) {
        let template = null;
        if (simpleMap[simpleList[i].id]) {
          template = simpleMap[simpleList[i].id];
        } else {
          template = util.clone(simple);
          template.id = simpleList[i].id;
          template.data.id = simpleList[i].id;
        }
        for (let j = template.data.file.length - 1; j >= 0; j--) {
          template.data.file.splice(j, 1);
        }
        for (let j = 0; j < simpleList[i].child.length; j++) {
          template.data.file.splice(i, 0, simpleList[i].child[j]);
        }
        this.simpleList.push({
          id: simpleList[i].id,
          component: "Simple",
          icon: simpleIcon,
          label: simpleList[i].name,
        });
        this.baseElementList.splice(i, 0, template);
        this.baseElementMap[template.id] = template;
      }
    }
  },
  removeResource: function (id, type = ["01", "02"]) {
    let data = url.deleteResource({ token: user.getToken(), id: id });
    if (data.state.code == 10000) {
      this.resetResourceList(type);
      if (this.message) {
        this.message("删除资源成功！", "success");
      }
    } else {
      if (this.message) {
        this.message(data.body.message, "error");
      }
    }
  },
  removeElement: function (id) {
    let data = url.deleteSimple({ token: user.getToken(), id: id });
    if (data.state.code == 10000) {
      this.resetSimpleList();
      if (this.message) {
        this.message("删除资源成功！", "success");
      }
    } else {
      if (this.message) {
        this.message("删除资源失败！", "error");
      }
    }
  },
  removeSimpleFile: function (id, successFun, errorFun) {
    let data = url.deleteSimpleFile({ token: user.getToken(), id: id });
    if (data.state.code == 10000) {
      this.resetSimpleList();
      if (this.message) {
        this.message("删除自定义组件文件成功！", "success");
        successFun();
      }
    } else {
      if (this.message) {
        this.message("删除自定义组件文件失败！", "error");
        errorFun();
      }
    }
  },
};
export default template;
